package com.foreknow.hospital.framework;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 自定义一个前端控制器，主要用来拦截url请求 格式要求：/控制器类名/控制器方法名Co
 * 流程：
 * 1. 中文编码的处理
 * 2. 获取客户端请求的路径(/Controller类名/Controller方法名)
 *    请求的url与Controller方法映射：http://localhost:8080/elm_project/Controller类名/Controller方法名
 * 3. 根据获取到的请求路径来获取到Controller类名和Controller方法名
 * 4. 调用Controller中的方法
 * 5. 最后将获取到的数据以json的格式返回给客户端
 */
@WebServlet("/")
public class DispatcherServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 中文编码的处理
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //设置服务器端向客户端返回的内容的类型
        response.setContentType("application/json;charset=utf-8");
        //2. 获取客户端请求的路径 获取到请求的URL的一部分 path="/Controller类名/Controller方法名"
        String path = request.getServletPath();
        //3. 根据获取到的请求路径来获取到Controller类名和Controller方法名 path="/Controller类名/Controller方法名"
        String className = path.substring(1,path.lastIndexOf("/"));
        String methodName = path.substring(path.lastIndexOf("/")+1);
        PrintWriter out = response.getWriter();
        try {
            //4. 通过Java中的反射机制调用Controller中的方法
            //可以通过Class.forName来获取Controller类中的信息
            Class clazz = Class.forName("com.foreknow.hospital.controller."+className);
            //创建Controller对象
            Object controller = clazz.newInstance();
            //获取Controller中的方法
            Method method = clazz.getMethod(methodName,new Class[]{HttpServletRequest.class});
            //调用Controller中的方法
            Object result = method.invoke(controller,new Object[]{request});
            //5. 最后将获取到数据以json的格式返回给客户端 jackson中提供的对象ObjectMapper
            ObjectMapper objectMapper = new ObjectMapper();
            out.print(objectMapper.writeValueAsString(result));

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
